/*-
 * Copyright (c) 2011 Ondrej Fischer <ondrej.fischer@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
#ifndef EXPRESS_SHUTDOWN_HPP
#define EXPRESS_SHUTDOWN_HPP


namespace express {
    
    
    /**
     * Class Shutdown
     * 
     * This is a special job to shutdown all processing queues of the system.
     * It consists of one single task, that is satisfied, when all requested
     * jobs are done. Then it sends a special ending tasks to each queue, which
     * cycles in it until all threads receive it and finish.
     */
    class Shutdown : public Job {
        
    public:

        /**
         * Class EndOfQueue
         * 
         * This is a special task, that signals end of the queue. Any thread,
         * that receives it, have to shutdown. This task resubmits itself back
         * to the queue, so all threads reading from the queue will get it.
         */
        class EndOfQueue : public Job::Task {

        public:

            EndOfQueue(Job& j, Operation& o): Job::Task(j, o) {}

            /**
             * This method gets tested, so it first resubmits itself and then
             * indicating EOQ to the caller.
             */
            virtual bool quit() {trySubmit(); return true;}

        };

    private:
        
        Job::Task shutdown;

    public:

        /**
         * Shutdown constructor gets one queue (no matter which one, because at
         * time of submitting no queue is busy) as initial, where the shutdown
         * task is submitted.
         */
        Shutdown(SubmitService::Target& target)
        : Job("shutdown"), shutdown(*this, *new Job::Noop(target))
        {
            add(target);
        }


        /**
         * Method to register additional queues to shut down.
         */
        void add(SubmitService::Target& target) {
            (new EndOfQueue(*this, *new Job::Noop(target)))->require(shutdown);
        }
    };


}


#endif // EXPRESS_SHUTDOWN_HPP
